package com.example.rabc.controller;

import com.example.rabc.common.ApiResponse;
import com.example.rabc.entity.Role;
import com.example.rabc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 角色控制器
 */
@RestController
@RequestMapping("/api/roles")
public class RoleController {
    
    @Autowired
    private RoleService roleService;

    /**
     * 创建角色
     * @param role 角色对象
     * @param permissionCodes 权限代码列表
     * @return 包含创建角色的ApiResponse对象
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Role>> createRole(@RequestBody Role role, 
                                                        @RequestParam(required = false) Set<String> permissionCodes) {
        Role createdRole = roleService.createRole(role, permissionCodes);
        return ResponseEntity.ok(ApiResponse.success(createdRole));
    }

    /**
     * 更新角色
     * @param roleId 角色ID
     * @param role 角色对象
     * @param permissionCodes 权限代码列表
     * @return 包含更新角色的ApiResponse对象
     */
    @PutMapping("/{roleId}")
    public ResponseEntity<ApiResponse<Role>> updateRole(@PathVariable Long roleId, 
                                                        @RequestBody Role role,
                                                        @RequestParam(required = false) Set<String> permissionCodes) {
        Role updatedRole = roleService.updateRole(roleId, role, permissionCodes);
        return ResponseEntity.ok(ApiResponse.success(updatedRole));
    }

    /**
     * 删除角色
     * @param roleId 角色ID
     * @return 包含删除结果的ApiResponse对象
     */
    @DeleteMapping("/{roleId}")
    public ResponseEntity<ApiResponse<Void>> deleteRole(@PathVariable Long roleId) {
        roleService.deleteRole(roleId);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    /**
     * 根据角色ID获取角色
     * @param roleId 角色ID
     * @return 包含角色对象的ApiResponse对象
     */
    @GetMapping("/{roleId}")
    public ResponseEntity<ApiResponse<Role>> getRoleById(@PathVariable Long roleId) {
        Role role = roleService.getRoleById(roleId);
        return ResponseEntity.ok(ApiResponse.success(role));
    }

    /**
     * 根据角色代码获取角色
     * @param roleCode 角色代码
     * @return 包含角色对象的ApiResponse对象
     */
    @GetMapping("/code/{roleCode}")
    public ResponseEntity<ApiResponse<Role>> getRoleByCode(@PathVariable String roleCode) {
        Role role = roleService.getRoleByCode(roleCode);
        return ResponseEntity.ok(ApiResponse.success(role));
    }

    /**
     * 获取所有角色
     * @return 包含角色列表的ApiResponse对象
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<Role>>> getAllRoles() {
        List<Role> roles = roleService.getAllRoles();
        return ResponseEntity.ok(ApiResponse.success(roles));
    }

    /**
     * 获取所有激活角色
     * @return 包含角色列表的ApiResponse对象
     */
    @GetMapping("/active")
    public ResponseEntity<ApiResponse<List<Role>>> getActiveRoles() {
        List<Role> roles = roleService.getActiveRoles();
        return ResponseEntity.ok(ApiResponse.success(roles));
    }

    /**
     * 为角色分配权限
     * @param roleId 角色ID
     * @param permissionCodes 权限代码列表
     * @return 包含分配结果的ApiResponse对象
     */
    @PostMapping("/{roleId}/permissions")
    public ResponseEntity<ApiResponse<Boolean>> assignPermissions(@PathVariable Long roleId, 
                                                                  @RequestBody Set<String> permissionCodes) {
        boolean result = roleService.assignPermissionsToRole(roleId, permissionCodes);
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    /**
     * 从角色中移除权限
     * @param roleId 角色ID
     * @param permissionCodes 权限代码列表
     * @return 包含移除结果的ApiResponse对象
     */
    @DeleteMapping("/{roleId}/permissions")
    public ResponseEntity<ApiResponse<Boolean>> removePermissions(@PathVariable Long roleId, 
                                                                  @RequestBody Set<String> permissionCodes) {
        boolean result = roleService.removePermissionsFromRole(roleId, permissionCodes);
        return ResponseEntity.ok(ApiResponse.success(result));
    }
}